package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.Log;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.system.entity.DrawlingDetail;
import cc.mrbird.febs.system.service.IDrawlingDetailService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author MrBird
 * @date 2019-07-22 11:18:22
 */
@Slf4j
@Validated
@Controller

public class DrawlingDetailController extends BaseController {

    @Autowired
    private IDrawlingDetailService drawlingDetailService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "drawlingDetail")
    private String drawlingDetailIndex(){
        return FebsUtil.view("drawlingDetail/drawlingDetail");
    }

    @GetMapping("drawlingDetail")
    @ResponseBody
    public FebsResponse getAllDrawlingDetails(DrawlingDetail drawlingDetail) {
        return new FebsResponse().success().data(drawlingDetailService.findDrawlingDetails(drawlingDetail));
    }

    @PostMapping("drawlingDetail/list")
    @ResponseBody
//    @RequiresPermissions("drawlingDetail:list")
    public FebsResponse drawlingDetailList(QueryRequest request, DrawlingDetail drawlingDetail) {
        Map<String, Object> dataTable = getDataTable(this.drawlingDetailService.findDrawlingDetails(request, drawlingDetail));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("新增DrawlingDetail")
    @PostMapping("drawlingDetail")
    @ResponseBody
    //@RequiresPermissions("drawlingDetail:add")
    public FebsResponse addDrawlingDetail(@Valid DrawlingDetail drawlingDetail) throws FebsException {
        try {
            this.drawlingDetailService.saveOrUpdate(drawlingDetail);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增DrawlingDetail失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("删除DrawlingDetail")
    @GetMapping("drawlingDetail/delete")
    @RequiresPermissions("drawlingDetail:delete")
    public FebsResponse deleteDrawlingDetail(DrawlingDetail drawlingDetail) throws FebsException {
        try {
            this.drawlingDetailService.deleteDrawlingDetail(drawlingDetail);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除DrawlingDetail失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @Log("修改DrawlingDetail")
    @PostMapping("drawlingDetail/update")
    @RequiresPermissions("drawlingDetail:update")
    public FebsResponse updateDrawlingDetail(DrawlingDetail drawlingDetail) throws FebsException {
        try {
            this.drawlingDetailService.updateDrawlingDetail(drawlingDetail);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改DrawlingDetail失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @PostMapping("drawlingDetail/excel")
    @RequiresPermissions("drawlingDetail:export")
    public void export(QueryRequest queryRequest, DrawlingDetail drawlingDetail, HttpServletResponse response) throws FebsException {
        try {
            List<DrawlingDetail> drawlingDetails = this.drawlingDetailService.findDrawlingDetails(queryRequest, drawlingDetail).getRecords();
            ExcelKit.$Export(DrawlingDetail.class, response).downXlsx(drawlingDetails, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
